﻿function initEmailBox(){
    $(".close2").live("click",function(){
        $(this).parent().remove();
    });
}
function emailBox(div){
    $(div).html("");
    var input = $('<input class="hidden2" type="text" />');
    $(div).append(input);
    focusOutInput(input);
    $(div).click(function(){
        $(this).find("input").focus();
    });
    
    //Evento do teclado
    var input_component = $(input).find("input");
    $(input).keydown(function(e){
    
        //alert(e.keyCode);
        if((e.keyCode == 188 || e.keyCode == 110 || e.keyCode == 32)){
            var valor = $(this).val();
            if(IsEmail(valor)){
                box = $('<span class="email"><span>'+$(this).val()+' </span><button type="button" class="close close2">×</button></span>');
                $(this).before(box);
                $(this).val('');
                return false;
            }
            else{
                //Verificando se foi colado uma lista de email
                var stringOriginal = $(this).val();
                stringOriginal = replaceAll(stringOriginal, " ", "");
                var email = stringOriginal.split(",");
                for(var i in email){
                    if(IsEmail(email[i])){
                        box = $('<span class="email"><span>'+email[i]+' </span><button type="button" class="close close2">×</button></span>');
                        $(this).before(box);
                        stringOriginal = stringOriginal.replace(email[i],"");
                        if(stringOriginal[0] == ",")
                            stringOriginal = stringOriginal.replace(",","");
                    }
                }
                $(this).val("");
                
                return false;
            }
        }
    });
}

function IsEmail(email){
    var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
    var check=/@[\w\-]+\./;
    var checkend=/\.[a-zA-Z]{2,3}$/;
    if(((email.search(exclude) != -1)||(email.search(check)) == -1)||(email.search(checkend) == -1)){return false;}
    else {return true;}
}
function focusOutInput(input){
     //Verificando se foi colado uma lista de email
    $(input).blur(function(){
        var stringOriginal = $(this).val();
        stringOriginal = replaceAll(stringOriginal, " ", "");
        var email = stringOriginal.split(",");
        for(var i in email){
            if(IsEmail(email[i])){
                box = $('<span class="email"><span>'+email[i]+' </span><button type="button" class="close close2">×</button></span>');
                $(this).before(box);
                stringOriginal = stringOriginal.replace(email[i],"");
                if(stringOriginal[0] == ",")
                    stringOriginal = stringOriginal.replace(",","");
            }
        }
        $(this).val(stringOriginal);
        
    });
}
function getEmails(div){
    var array_email = new Array();
    $(div).children("span").each(function(){
        array_email.push($(this).children("span").html());
    });
    var value_input = $(div).find("input").val();
    if(IsEmail(value_input))
        array_email.push(value_input);
    
   return array_email;
} 

function replaceAll(str, de, para){
    var pos = str.indexOf(de);
    while (pos > -1){
		str = str.replace(de, para);
		pos = str.indexOf(de);
	}
    return (str);
}
